package com.dgut.epidemic.service;

import cn.hutool.core.util.StrUtil;
import com.dgut.epidemic.mapper.AdminDataMapper;
import com.dgut.epidemic.pojo.Daily_data;
import com.dgut.epidemic.pojo.abnormalData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class AdminDataServiceImpl implements AdminDataService{

    @Autowired
    private AdminDataMapper adminDataMapper;

    public PageInfo<Daily_data> sortData(int pageNum, int pageSize, String sort,String starTime,String endTime
            ,String body,String istreatment,String isolated,String inschool,String inguan,String keyword){
        PageHelper.startPage(pageNum, pageSize);
        List<Daily_data> daily_data = adminDataMapper.sortData(sort,starTime,endTime,body,istreatment,isolated,inschool,inguan,keyword);
        PageInfo<Daily_data> pageInfo = new PageInfo<>(daily_data);
        return pageInfo;
    };

    public PageInfo<Daily_data> selectData(int pageNum, int pageSize){
        PageHelper.startPage(pageNum, pageSize);

        PageInfo<Daily_data> pageInfo = new PageInfo<>(adminDataMapper.selectData());
        return pageInfo;
    }

    public List<Daily_data> riskAreaData(String starTime,String endTime) throws IOException {
        List<Daily_data> daily_data = adminDataMapper.sortData("",starTime,endTime,"","","","","","");
        List<Daily_data> result_data = new ArrayList<Daily_data>();

        Document document = Jsoup.connect("http://m.dg.bendibao.com/news/gelizhengce/fengxianmingdan.php").get();
        Elements elements1 = document.select(".height").select("span");
        Elements elements2 = document.select(".middle").select("span");
        List<String> list = new ArrayList<String>();
        for (Element element : elements1) {
            if(element.text().length()>8){
                list.add(element.text());
            }
        }
        for (Element element : elements2) {
            if(element.text().length()>8){
                list.add(element.text());
            }
        }

        for(Daily_data dd:daily_data){
            for(String str : list){
                if(str.contains(StrUtil.removeAny(dd.getLocation(),"河北省","山西省","辽宁省","吉林省","黑龙江省","江苏省"
                        ,"浙江省","安徽省","福建省","江西省","山东省","河南省","湖北省","湖南省","广东省","海南省","四川省","贵州省","云南省"
                        ,"陕西省","甘肃省","青海省","台湾省","内蒙古自治区","广西壮族自治区","西藏自治区","宁夏回族自治区","新疆维吾尔自治区"
                        ,"北京市","天津市","上海市","重庆市","香港特别行政区","澳门特别行政区"))){
                    result_data.add(dd);
                    break;
                }
            }
        }

        return result_data;

    };



    public List<String> heightRiskArea() throws IOException {
        Document document = Jsoup.connect("http://m.dg.bendibao.com/news/gelizhengce/fengxianmingdan.php").get();
        Elements elements1 = document.select(".height").select("span");
        List<String> list1 = new ArrayList<String>();//高风险地区
        for (Element element : elements1) {
            if(element.text().length()>8){
                list1.add(element.text());
            }
        }
        return list1;
    }

    public List<String> middleRiskArea() throws IOException {
        Document document = Jsoup.connect("http://m.dg.bendibao.com/news/gelizhengce/fengxianmingdan.php").get();
        Elements elements2 = document.select(".middle").select("span");
        List<String> list2 = new ArrayList<String>();//中风险地区
        for (Element element : elements2) {
            if(element.text().length()>8){
                list2.add(element.text());
            }
        }
        return list2;
    }

    public List<Daily_data> selectTemperature(String starTime,String endTime) {
        return adminDataMapper.selectTemperature(starTime,endTime);
    }

    public List<Daily_data> excelSelect(String sort,String starTime,String endTime,String body
            ,String istreatment,String isolated,String inschool,String inguan,String keyword){
        return adminDataMapper.sortData(sort,starTime,endTime,body,istreatment,isolated,inschool,inguan,keyword);
    }

    public List<abnormalData> selectAbnormal(){
        return adminDataMapper.selectAbnormal();
    }
}
